
package system.base;

import framework.core.*;
import system.SelectManager;
import system.data.*;
/*
	Author    :Wing
	CreateTime:2008-01-30 14:11:28
*/
public class CustomerSrv extends IHandler
{

	public CustomerSrv() 
	{
		super();
	}
	private void deleteCustomer(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		Customer obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = Customer.fetch(con,ids[i]);
			if(!obj.getWarehouseCode().equals(context.getSessionValue("LoginWarehouseCode")))
			    throw new BizException("您无权进行该操作!");
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveCustomer(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		Customer obj;
		
		CustomerPageData data = (CustomerPageData)PageData.fromForm(context,CustomerPageData.class);
		if((id==null)||(id.equals("")))   //add
		{
			obj = Customer.fetch();
			data.setCreateUserId(loginContext.getLoginUserId());
            data.setCreateUser(loginContext.getLoginUserName());
    	}
    	else                     //update
    	{
    		obj = Customer.fetch(con,id);
    	}
		data.setLastUpdateUserId(loginContext.getLoginUserId());
        data.setLastUpdateUser(loginContext.getLoginUserName());
		
		context.setValue("CustomerPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listCustomer(DBConnection con,SrvContext context,ILoginContext loginContext,String path) throws Exception
	{
		CustomerSearchEvent event = (CustomerSearchEvent)SearchEvent.fromForm(context,CustomerSearchEvent.class);
		//event.setWarehouseCodeUser(ConstDefine.getWildCard(loginContext.getLoginWarehouseCode()));
		context.setSessionValue("CustomerSearchEvent",event);
		context.setNextUrl(path+"CustomerList.jsp");
	}
	private void backCustomer(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"CustomerList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listCustomer"))
		{
	        context.setSessionValue("LoginWarehouseCode", loginContext.getLoginWarehouseCode());
			listCustomer(con,context,loginContext,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewCustomer"))
		{
			String id = context.getString("Id",null);
			Customer obj = Customer.fetch(con,id);
			context.setValue("Customer",obj);
			Customer com = null;
			if (!obj.getUpperCustomerCode().equalsIgnoreCase("")) {
			    CustomerSearchEvent event = new CustomerSearchEvent();
                event.setCode(obj.getUpperCustomerCode());
                com = Customer.fetch(con, event.toConditions());
            }
			context.setValue("UpperCustomer",com);
			context.setNextUrl(path+"CustomerView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteCustomer"))
		{
			context.setErrorUrl(path+"CustomerList.jsp");
			deleteCustomer(con,context);
			backCustomer(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddCustomer"))
		{
		    context.setErrorUrl(path+"CustomerList.jsp");
			Customer obj = Customer.fetch();
			CustomerPageData data = (CustomerPageData)PageData.fromTable(obj,CustomerPageData.class);
			context.setValue("CustomerPageData",data);
			String strCustomerId = context.getString("Id",null);
			Customer com = Customer.fetch(con, strCustomerId);
			context.setValue("UpperCustomer",com);
			/*
			if(!com.getWarehouseCode().equals(loginContext.getLoginWarehouseCode()))
			{
			    context.setErrorUrl(path+"CustomerList.jsp");
                throw new BizException("您无权进行该操作!");			
			}
			*/
			context.setNextUrl(path+"CustomerEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateCustomer"))
		{
		    context.setErrorUrl(path+"CustomerList.jsp");
		    
		    
		    /** 郑斌 2008-03-19 11:35       开始添加代码:*/
            String[] ids = context.getParameterValues("ids");
            Customer temp = null;
            String code1 = "";
            String code2 = "";
            for(int i=0;i<ids.length;i++)
            {
                temp = Customer.fetch(con,ids[i]);
                code1 = temp.getWarehouseCode();
                code2 = String.valueOf(context.getSessionValue("LoginWarehouseCode"));
                boolean result = code1.equals(code2);
                if(!result)
                    throw new BizException("您无权进行该操作!");
            }
            /** 郑斌 2008-03-19 11:42       结束添加代码;*/
            
            
			String id = context.getString("Id",null);
			Customer obj = Customer.fetch(con,id);
			CustomerPageData data = (CustomerPageData)PageData.fromTable(obj,CustomerPageData.class);
			context.setValue("CustomerPageData",data);
			Customer com = null;
			if (!obj.getUpperCustomerCode().equalsIgnoreCase("")) {
    			CustomerSearchEvent event = new CustomerSearchEvent();
    			event.setCode(obj.getUpperCustomerCode());
    			com = Customer.fetch(con, event.toConditions());
			}
			if (com == null) com = new Customer();
			context.setValue("UpperCustomer",com);
			//System.out.println("---------------------------------------"+com.getWarehouseCode());
			//System.out.println("---------------------------------------"+context.getSessionValue("LoginWarehouseCode"));
			//if(!com.getWarehouseCode().equals(context.getSessionValue("LoginWarehouseCode")))
            //    throw new BizException("您无权进行该操作!");
			context.setNextUrl(path+"CustomerEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyCustomer"))
		{
			String id = context.getString("Id",null);
			Customer obj = Customer.copy(Customer.fetch(con,id));
			CustomerPageData data = (CustomerPageData)PageData.fromTable(obj,CustomerPageData.class);
			context.setValue("CustomerPageData",data);
			context.setNextUrl(path+"CustomerEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveCustomer"))
		{
			context.setErrorUrl(path+"CustomerList.jsp");
			saveCustomer(con,context,loginContext);
			backCustomer(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewCustomer"))
		{
			context.setErrorUrl(path+"CustomerList.jsp");
			saveCustomer(con,context,loginContext);		
	        String strCustomerId = context.getString("UpperCustomerId",null);
	        Customer com = new Customer();
	        if(strCustomerId != null)
	        {	            
	            com = Customer.fetch(con, strCustomerId);	           
	        }
	        if (com == null) com = new Customer();
	        context.setValue("UpperCustomer",com);
			Customer obj = Customer.fetch();
			CustomerPageData data = (CustomerPageData)PageData.fromTable(obj,CustomerPageData.class);
			context.setValue("CustomerPageData",data);
			context.setNextUrl(path+"CustomerEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListCustomer"))
		{
			backCustomer(con,context,path);
			return;
		}
		
		Logger.throwException("CustomerSrv","unknown action_code="+action_code);
	}
}
